﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Cemit.NetFramework.Editor
{
    /// <summary>
    /// 在编辑器下对文件进行读写
    /// </summary>
#if UNITY_EDITOR
    [InitializeOnLoad]
#endif
    public class PolyPath
    {
        public static string Root => Path.Combine("Assets", "PolyNetwork");
        public static string Resources => Path.Combine(Root, "Resources");

        PolyPath() { }

#if UNITY_EDITOR

        static PolyPath()
        {
            Initialize();
        }

        public static Object CreateAsset(Object asset, string path)
        {
            Initialize();
            AssetDatabase.CreateAsset(asset, path);
            return asset;
        }

        public static T LoadAsset<T>(string path) where T : Object
        {
            return AssetDatabase.LoadAssetAtPath<T>(path);
        }

        private static PolyPath Initialize()
        {
            foreach (var item in typeof(PolyPath).GetProperties())
            {
                CheckAndCreateDirectory((string)item.GetValue(typeof(PolyPath)));
            }
            return new PolyPath();
        }

        private static void CheckAndCreateDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

#endif

    }
}